/*******************************************************************************
 * Copyright (c) 2004 BlueOxygen Technology.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the BlueOxygen Software License v1.0
 * which accompanies this distribution, and is available at
 *
 * Contributors:
 *     BlueOxygen Team - initial API and implementation
 *******************************************************************************/

package org.blueoxygen.cimande;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.rest.DefaultHttpHeaders;
import org.blueoxygen.cimande.persistence.PersistenceManager;
import org.blueoxygen.cimande.persistence.hibernate.HibernateSessionFactory;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author Dian Aditya
 *
 */
public class CimandeAction extends ActionSupport implements
		ServletRequestAware, ServletResponseAware {

	public static final String INDEX = "index";
	public static final String SHOW = "show";
	public static final String NEW = "new";
	public static final String EDIT = "edit";
	public static final String DELETE = "delete";

	@Autowired
	protected PersistenceManager manager;
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected DefaultHttpHeaders headers = new DefaultHttpHeaders();
	protected Map<String, String> actions = new HashMap<String, String>();

	@Autowired
	protected HibernateSessionFactory hsf;
	protected Session session;

	private static Properties properties = new Properties();
	protected String id = "";

	@PostConstruct
	public void init() {
		session = hsf.createSession();
	}

	@PreDestroy
	public void destroy() {
		session.flush();
	}

	public String getParameter(int index) {
		index += 1;
		String[] temp = request.getServletPath().split("/");
		if (index < 1 || index >= temp.length)
			return null;

		return temp[index];
	}

	public static String get(String propertyName) {
		return properties.getProperty(propertyName);
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

}
